package com.realw.argus.web.common.velocity;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.context.Context;
import org.apache.velocity.tools.Scope;
import org.apache.velocity.tools.ToolboxFactory;
import org.apache.velocity.tools.config.ConfigurationUtils;
import org.apache.velocity.tools.view.ViewToolContext;
import org.springframework.web.servlet.view.velocity.VelocityToolboxView;

/**
 * 支持Velocity Tool 2.0
 * @author NeoArgus
 */
public class VelocityToolboxV2View extends VelocityToolboxView {

	@Override
	protected Context createVelocityContext(Map<String, Object> model,
			HttpServletRequest request, HttpServletResponse response) {

		ViewToolContext context = new ViewToolContext(getVelocityEngine(),
				request, response, getServletContext());

		if (model != null) {
			context.putAll(model);
		}

		ToolboxFactory factory = new ToolboxFactory();
		factory.configure(ConfigurationUtils
				.findInClasspath(getToolboxConfigLocation()));

		for (String scope : Scope.values()) {
			context.addToolbox(factory.createToolbox(scope));
		}

		return context;
	}

}
